package com.hsck.order.kds.main.fragment.record

import android.app.Activity
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.AsyncListDiffer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.hsck.order.kds.R
import com.hsck.order.kds.base.utils.setOnSingleClickListener
import com.hsck.order.kds.databinding.FoodDeliveryRecordHrItemLayoutBinding
import com.hsck.order.kds.databinding.OrderDynamicsDishItemsLayoutBinding
import com.hsck.order.kds.main.fragment.standard.bean.TestListBean

class FoodDeliveryRecordHrItemsAdapter(val activity: Activity,val columns:Int) :
    RecyclerView.Adapter<FoodDeliveryRecordHrItemsAdapter.ItemVH>() {

    private val diffUtil = object : DiffUtil.ItemCallback<TestListBean>() {
        override fun areItemsTheSame(oldItem: TestListBean, newItem: TestListBean): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: TestListBean, newItem: TestListBean): Boolean {
            return oldItem == newItem
        }
    }
    private val asyncListDiffer = AsyncListDiffer(this, diffUtil)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemVH {
        return ItemVH(
            FoodDeliveryRecordHrItemLayoutBinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            )
        )
    }

    inner class ItemVH(val binding: FoodDeliveryRecordHrItemLayoutBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bindData(position: Int) {
            val item = asyncListDiffer.currentList[position]
//            binding.bean = item
//            binding.tvTime.text = calculateMinutesSinceOrder(item.time).toString()
        }
    }

    fun submitList(dataResponse: List<TestListBean>) {
        asyncListDiffer.submitList(dataResponse)
    }

    fun addItemAfterId(newItem: TestListBean, targetId: String) {
        val newList = asyncListDiffer.currentList.toMutableList()
        val targetIndex = newList.indexOfFirst { it.id == targetId }
        if (targetIndex != -1) {
            newList.add(targetIndex, newItem)
            asyncListDiffer.submitList(newList)
        }
    }

    fun removeItemAfterId(targetId: String) {
        val newList = asyncListDiffer.currentList.toMutableList()
        val targetIndex = newList.indexOfFirst { it.id == targetId }
        if (targetIndex != -1) {
            newList.removeAt(targetIndex)
            asyncListDiffer.submitList(newList)
        }
    }

    fun removeItemAfterIndex(targetIndex: Int) {
        if (targetIndex < asyncListDiffer.currentList.size) {
            val newList = asyncListDiffer.currentList.toMutableList()
            newList.removeAt(targetIndex)
            asyncListDiffer.submitList(newList)
        }
    }

    override fun onBindViewHolder(holder: ItemVH, position: Int) {
        holder.bindData(position)
    }

    override fun getItemCount(): Int {
        return asyncListDiffer.currentList.size
    }
}